﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Enterprise.Model.Note;
using Enterprise.Data.Note;
using Enterprise.Infrastructure;
namespace Enterprise.Service.Note
{
    /// <summary>
    /// 个人备忘or记事服务
    /// </summary>
    public class NoteService
    {

        /// <summary>
        /// 缓存标记
        /// </summary>
        private static string cacheKey = "Enterprise-Note";

        /// <summary>
        /// 列表 返回所有记录
        /// </summary>
        /// <returns></returns>
        public List<NoteEntity> NoteList()
        {
            if (CacheHelper.Contains(cacheKey))
            {
                return (List<NoteEntity>)CacheHelper.GetCache(cacheKey);
            }
            else
            {
                NoteData data = new NoteData();
                List<NoteEntity> list = data.NoteList().OrderByDescending(p=>p.NOTEID).ToList();
                CacheHelper.Add(cacheKey, list);
                return list;
            }
        }

        /// <summary>
        /// 返回记录
        /// </summary>
        /// <param name="odId"></param>
        /// <returns></returns>
        public NoteEntity NoteDisp(int noteId)
        {
            return NoteList().Where(p => p.NOTEID == noteId).FirstOrDefault();
        }

        /// <summary>
        /// 清除缓存
        /// </summary>
        private void ClearCache()
        {
            CacheHelper.RemoveCache(cacheKey);
        }

        /// <summary>
        /// 操作 添加、修改、删除
        /// </summary>
        /// <param name="entity">用户实体</param>
        public void NoteExecute(NoteEntity entity)
        {
            string cmd = string.Empty;
            switch (entity.DB_Option_Action)
            {
                case "Insert":
                    cmd = "Insert into SYS_NOTE(NOTEID,NTITLE,NREMARK,NISREMIND,NREMINDUSERS,NREMINDTIME,NREMINDTYPE,NUSERID,NCREATETIME) values(SEQ_SYS_NOTE.nextval,'" + entity.NTITLE + "','" + entity.NREMARK + "'," + entity.NISREMIND + ",'"+entity.NREMINDUSERS+"'," + Utility.FormatOracleDate(entity.NREMINDTIME) + ",'" + entity.NREMINDTYPE + "'," + entity.NUSERID + "," + Utility.FormatOracleDate(entity.NCREATETIME) + ")";
                    break;
                case "Update":
                    cmd = "Update SYS_NOTE set NTITLE='" + entity.NTITLE + "' ,NREMARK='" + entity.NREMARK + "',NISREMIND=" + entity.NISREMIND + ",NREMINDUSERS='"+entity.NREMINDUSERS+"',NREMINDTIME=" + Utility.FormatOracleDate(entity.NREMINDTIME) + ",NREMINDTYPE='" + entity.NREMINDTYPE + "' where NOTEID=" + entity.NOTEID;
                    break;
                case "Delete":
                    cmd = "Delete from SYS_NOTE where NOTEID=" + entity.NOTEID;
                    break;
            }
            if (!string.IsNullOrEmpty(cmd))
            {
                NoteExecuteBySql(cmd, 1);
            }
        }

        /// <summary>
        /// 操作方法
        /// </summary>
        /// <param name="cmd"></param>
        /// <param name="typeId">1:刷新缓存 0:不刷新缓存 </param>
        public void NoteExecuteBySql(string cmd, int typeId)
        {
            NoteData data = new NoteData();
            data.Execute(cmd);
            if (typeId == 1)
            {
                ClearCache();
            }
        }

    }
}
